var dataBase = null; /*暂存数据库对象*/
/**
 * 打开数据库
 * @returns  dataBase:打开成功   null:打开失败
 */
function CrmOpenDB(){
    /*数据库有就打开 没有就创建*/
    dataBase = window.openDatabase('crm', '1.0',  'crm临时表', 10*1024*1024 ,function() {});
    if (dataBase) {
        
    } else{

    }
    CrmCreatTable();
};

function CrmCreatTable(){
    var creatTableSQL = 'CREATE TABLE IF  NOT EXISTS user (rowid INTEGER PRIMARY KEY AUTOINCREMENT, ID INTEGER,RealName text,EnName text,FirstChar text,HeadImg text,BigHeadImg text,RoleName text,DepartName text,UserName text,Sex text,BirthPlace text,Birthday text,JoinDate text,Email text,QQ text,School text,SchoolDate text,SchoolType text,Profession text)';
    dataBase.transaction(function (ctx,result) {
        ctx.executeSql(creatTableSQL,[],function(ctx,result){
        },function(tx, error){ 
        });
    });
};

function CrmInsterData(ID,RealName,EnName,FirstChar,HeadImg,BigHeadImg,RoleName,DepartName,UserName,Sex,BirthPlace,Birthday,JoinDate,Email,QQ,School,SchoolDate,SchoolType,Profession){
    var insterTableSQL = 'INSERT INTO user (ID,RealName,EnName,FirstChar,HeadImg,BigHeadImg,RoleName,DepartName,UserName,Sex,BirthPlace,Birthday,JoinDate,Email,QQ,School,SchoolDate,SchoolType,Profession) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
    dataBase.transaction(function (ctx) {
        ctx.executeSql(insterTableSQL,[ID,RealName,EnName,FirstChar,HeadImg,BigHeadImg,RoleName,DepartName,UserName,Sex,BirthPlace,Birthday,JoinDate,Email,QQ,School,SchoolDate,SchoolType,Profession],function (ctx,result){
        },
        function (tx, error) {
        });
    });
};

function CrmUpdateData(ID,RealName,EnName,FirstChar,HeadImg,BigHeadImg,RoleName,DepartName,UserName,Sex,BirthPlace,Birthday,JoinDate,Email,QQ,School,SchoolDate,SchoolType,Profession){
    var updateDataSQL = 'UPDATE user SET RealName = ?,EnName=?,FirstChar=?,HeadImg=?,BigHeadImg=?,RoleName=?,DepartName=?,UserName=?,Sex=?,BirthPlace=?,Birthday=?,JoinDate=?,Email=?,QQ=?,School=?,SchoolDate=?,SchoolType=?,Profession=? WHERE ID = ?';
    dataBase.transaction(function (ctx,result) {
        ctx.executeSql(updateDataSQL,[RealName,EnName,FirstChar,HeadImg,BigHeadImg,RoleName,DepartName,UserName,Sex,BirthPlace,Birthday,JoinDate,Email,QQ,School,SchoolDate,SchoolType,Profession,ID],function(ctx,result){
        },function(tx, error){ 
        });
    });
};

function CrmDeleteData(ID){
    var deleteDataSQL = 'DELETE FROM user WHERE ID = ?';
    dataBase.transaction(function (ctx,result) {
        ctx.executeSql(deleteDataSQL,[ID],function(ctx,result){
        },function(tx, error){
        });
    });
};

function CrmGetData(callback){
	var userlist=[];
    var selectSQL = 'SELECT * FROM user'
    dataBase.transaction(function (ctx) {
        ctx.executeSql(selectSQL,[],function (ctx,result){
            var len = result.rows.length;
            for(var i = 0;i < len;i++) {
                userlist.push(result.rows.item(i));
            }
        	if ( typeof(callback) == 'function' ) callback(userlist);
        },
        function (tx, error) {
        });
        
    });
    
    
};

function CrmOneData(ID,callback){    
    var selectSQL = 'SELECT * FROM user WHERE ID = ?'
    dataBase.transaction(function (ctx) {
        ctx.executeSql(selectSQL,[ID],function (ctx,result){
            var data={};
            if(result.rows.length>0)
            {
            	data=result.rows.item(0);
            }
            if ( typeof(callback) == 'function' ) callback(data);
        },
        function (tx, error) {
        });
    });
};

function Update_UserMate(serveruserlist,localuserlist){
	var serveruserids=[];
	var localuserids=[];
	$.each(serveruserlist,function(i,info){
		serveruserids.push(info.ID);
	});
	$.each(localuserlist,function(i,info){
		localuserids.push(info.ID);
	});
	
	$.each(serveruserlist,function(i,info){
		if($.inArray(info.ID,localuserids)==-1)
		{
			CrmInsterData(info.ID,info.RealName,info.EnName,info.FirstChar,info.HeadImg,info.BigHeadImg,info.RoleName,info.DepartName,info.UserName,info.Sex,info.BirthPlace,info.Birthday,info.JoinDate,info.Email,info.QQ,info.School,info.SchoolDate,info.SchoolType,info.Profession);
		}else
		{
			CrmUpdateData(info.ID,info.RealName,info.EnName,info.FirstChar,info.HeadImg,info.BigHeadImg,info.RoleName,info.DepartName,info.UserName,info.Sex,info.BirthPlace,info.Birthday,info.JoinDate,info.Email,info.QQ,info.School,info.SchoolDate,info.SchoolType,info.Profession);
		}
	});
	
	$.each(localuserlist,function(i,info){
		if($.inArray(info.ID,serveruserids)==-1)
		{
			CrmDeleteData(info.ID);
		}
	});
};
